package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.NotesReviewMapper;
import com.chixing.mapper.TravelNotesMapper;
import com.chixing.pojo.*;
import com.chixing.service.TravelNotesService;
import com.chixing.util.DateUtil;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TravelNotesServiceImpl implements TravelNotesService {
    @Autowired
    private TravelNotesMapper travelNotesMapper;
    @Autowired
    private NotesReviewMapper notesReviewMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResponse getById(Integer notesId) {
        TravelNotes travelNotes=travelNotesMapper.selectByPrimaryKey(notesId);
        if(travelNotes!=null)
            return ServerResponse.getSuccess(travelNotes);
        return ServerResponse.getFail(null);
    }

    //添加游记
    @Override
    public ServerResponse save(String content, String title, Integer custId, String describe, Integer days, String startdate  ,String loc,String mainImgUrl) {

        TravelNotes travelNotes = new TravelNotes();
        byte[] bytes=content.getBytes();
        Date date = new Date();
        Date date1 = DateUtil.string2Date(startdate, DateUtil.DATE_PATTERN_YMDHMS);
        travelNotes.setTravelNotesText(bytes);
        travelNotes.setTravelNotesTitle(title);
        travelNotes.setCustId(custId);
        travelNotes.setTravelNotesOthrt1(describe);
        travelNotes.setTravelNotesDate(date);
        travelNotes.setTravelNotesTotaldays(days);
        travelNotes.setTravelNotesStartdate(date1);
        travelNotes.setTravelNotesOthrt2(loc);
        travelNotes.setTravelNotesMainImg(mainImgUrl);
        int rows=travelNotesMapper.insert(travelNotes);
        if (rows>0)
            return ServerResponse.saveSuccess(travelNotes);
        return ServerResponse.saveFail(travelNotes);
    }

    @Override
    public ServerResponse delete(Integer notesId) {
        System.out.println("000");
        int rows=travelNotesMapper.deleteByPrimaryKey(notesId);
        if(rows>0){
            return ServerResponse.deleteSuccess(rows);

        }
        else {
            return ServerResponse.deleteFail(null);
        }

    }

   /* @Override
    public ServerResponse getAllByCustId(Integer custId) {//根据用户id 查询全部游记
        TravelNotesExample example = new TravelNotesExample();
        example.createCriteria().andCustIdEqualTo(custId);
        List<TravelNotes> travelNotesList=travelNotesMapper.selectByExample(example);
        if (travelNotesList.size()>0)
            return ServerResponse.getSuccess(travelNotesList);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByPage(Integer pageNum) {
        PageHelper.startPage(pageNum,3);
        List<TravelNotes> travelNotesList=travelNotesMapper.selectByExample(null);
        PageInfo<TravelNotes> pageInfo=new PageInfo<>(travelNotesList);

        return ServerResponse.getSuccess(pageInfo);
    }*/

    @Override
    public ServerResponse getByCustIdPage(Integer custId, Integer pageNum) {//根据用户id 分页查询全部游记
        TravelNotesExample travelNotesExample=new TravelNotesExample();
        travelNotesExample.createCriteria().andCustIdEqualTo(custId);
        PageHelper.startPage(pageNum,3);
        List<TravelNotes> travelNotesList=travelNotesMapper.selectByExample(travelNotesExample);
        PageInfo<TravelNotes> pageInfo=new PageInfo<>(travelNotesList);
        if (travelNotesList!=null&&travelNotesList.size()!=0){
            return ServerResponse.getSuccess(pageInfo);
        }
        return ServerResponse.getFail(pageInfo);
    }

    @Override
    public List<NotesReviewAndCustomerVO> getByNotesId(Integer notesId) {
        NotesReviewExample notesReviewExample = new NotesReviewExample();
        notesReviewExample.createCriteria().andTravelNotesIdEqualTo(notesId);
        List<NotesReview> notesReviews = notesReviewMapper.selectByExample(notesReviewExample);
        List<NotesReviewAndCustomerVO> voList = new ArrayList<>();
        for (NotesReview review:notesReviews
             ) {
            NotesReviewAndCustomerVO vo = new NotesReviewAndCustomerVO();
            vo.setNotesReview(review);
            Customer customer = customerMapper.selectByPrimaryKey(review.getCustId());
            vo.setCustomer(customer);
            voList.add(vo);
        }
        return voList;
    }
}
